home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / µSim 1.0.5 / source / Dump.c < prev    next >
Encoding:
Text File  |  1995-11-01  |  11.2 KB  |  447 lines  |  [TEXT/CWIE]

  1. /*
  2. Copyright © 1993,1994,1995 Fabrizio Oddone
  3. ••• ••• ••• ••• ••• ••• ••• ••• ••• •••
  4. This source code is distributed as freeware:
  5. you may copy, exchange, modify this code.
  6. You may include this code in any kind of application: freeware,
  7. shareware, or commercial, provided that full credits are given.
  8. You may not sell or distribute this code for profit.
  9. */
  10.  
  11. //#pragma load "MacDump"
  12.  
  13. #include    "UtilsSys7.h"
  14. #include    "CursorBalloon.h"
  15. #include    "Conversions.h"
  16.  
  17. #include    "Disasm.h"
  18. #include    "Dump.h"
  19. #include    "DoEditDialog.h"
  20. #include    "DoMenu.h"
  21. #include    "Globals.h"
  22. #include    "Main.h"
  23. #include    "Input.h"
  24. #include    "Registers.h"
  25. #include    "Scroll.h"
  26. #include    "TrackThumb.h"
  27.  
  28.  
  29. #if defined(FabSystem7orlater)
  30.  
  31. //#pragma segment Main
  32.  
  33. ControlRef    dumpVScroll;
  34. static WindowRef    curPosW;
  35. short    dumpLineHeight, dumpFromTop, dumpCWidMax;
  36.  
  37. static void NoRealTimeActionProc(ControlHandle control, short value);
  38. static void RealTimeActionProc(ControlHandle control, short value);
  39. static pascal void DumpActionProc(ControlHandle control, short part);
  40. static void DrawLine(Ptr start);
  41. static short TrackDumpObject(RectPtr r, short theLoc);
  42.  
  43. void DumpHome(void)
  44. {
  45. SetControlValue(dumpVScroll, GetControlMinimum(dumpVScroll));
  46. InvalDump();
  47. }
  48.  
  49. void DumpEnd(void)
  50. {
  51. SetControlValue(dumpVScroll, GetControlMaximum(dumpVScroll));
  52. InvalDump();
  53. }
  54.  
  55. void DumpPgUp(void)
  56. {
  57. DumpActionProc(dumpVScroll, kControlPageUpPart);
  58. }
  59.  
  60. void DumpPgDn(void)
  61. {
  62. DumpActionProc(dumpVScroll, kControlPageDownPart);
  63. }
  64.  
  65. void Activate_Dump(EventRecord */*evt*/, WindowPtr w, Boolean becomingActive)
  66. {
  67. Rect    growRect;
  68.  
  69. /* the growbox needs to be redrawn on activation: */
  70. growRect = w->portRect;
  71. /* adjust for the scrollbars */
  72. growRect.top = growRect.bottom - kScrollbarAdjust + 1;
  73. growRect.left = growRect.right - kScrollbarAdjust + 1;
  74. if (becomingActive) {
  75.     InvalRect(&growRect);    /* we cannot avoid grow box flicker */
  76.     if ((*dumpVScroll)->contrlVis == 0) {
  77.         ShowControl(dumpVScroll);
  78.         ValidRect(&(*dumpVScroll)->contrlRect);
  79.         }
  80.     }
  81. else {
  82. /* the control must be redrawn on deactivation: */
  83.     HideControl(dumpVScroll);
  84.     InvalRect(&growRect);
  85.     }
  86. } /*Activate*/
  87.  
  88. void Grow_Dump(WindowPtr w, EventRecord *event)
  89. {
  90. Rect    tempRect, updateRect;
  91. register long    growResult;
  92.  
  93. tempRect.right = tempRect.left = PRCT_R(w) + 1;
  94. tempRect.bottom = SHRT_MAX;        /* set up limiting values */
  95. tempRect.top = kMinDocDim + 30;
  96. updateRect = w->portRect;
  97. updateRect.top = updateRect.bottom - kScrollbarAdjust;
  98. /* see if it really changed size */
  99. if (growResult = GrowWindow(w, event->where, &tempRect)) {
  100.     SizeWindow(w, LoWrd(growResult),
  101.                 (HiWrd(growResult) / dumpLineHeight) * dumpLineHeight, true);
  102.     SizeControl(dumpVScroll, kScrollbarWidth, PRCT_B(w) - PRCT_T(w) - 13);
  103.     if (HiWrd(growResult) > updateRect.bottom)
  104.         /* enlarged */
  105.         InvalRect(&updateRect);
  106.     else /* reduced */ {
  107.         updateRect.bottom = PRCT_B(w);
  108.         updateRect.right = PRCT_R(w);
  109.         updateRect.top = updateRect.bottom - kScrollbarAdjust + 1;
  110.         updateRect.left = updateRect.right - kScrollbarAdjust + 1;
  111.         InvalRect(&updateRect);
  112.         }
  113.     ValidRect(&(*dumpVScroll)->contrlRect);
  114.     SetupDumpCtlMax(dumpVScroll);
  115.     }
  116. }
  117.  
  118. void Update_Dump(WindowPtr w)
  119. {
  120. Rect    growRect;
  121. register RgnHandle    oldClip;
  122.  
  123. if (EmptyRgn(w->visRgn) == false) {    /* draw if updating needs to be done */
  124.     DrawDump(w);
  125.     oldClip = NewRgn();
  126.     GetClip(oldClip);
  127.     growRect = w->portRect;
  128.     growRect.left = growRect.right - kScrollbarAdjust;
  129.     ClipRect(&growRect);
  130.     DrawGrowIcon(w);
  131.     SetClip(oldClip);
  132.     DisposeRgn(oldClip);
  133.     UpdateControls(w, w->visRgn);
  134.     }
  135. }
  136.  
  137. void Do_Dump(WindowPtr w, EventRecord *event)
  138. {
  139. enum {
  140. kCurPosWindow = 128
  141. };
  142.  
  143. Rect    tempRect;
  144. GrafPtr    savePort;
  145. ControlHandle    control;
  146. Point    mouse;
  147. register unsigned long    clickAddr;
  148. register short    part, offset, popItem;
  149.  
  150. mouse = event->where;        /* get the click position */
  151. GlobalToLocal(&mouse);
  152. /* see if we are in the dump area; if so, we won’t check the controls */
  153. tempRect = w->portRect;
  154. tempRect.right -= kScrollbarAdjust;
  155. if (PtInRect(mouse, &tempRect)) {
  156.     /* handle editing click */
  157.     if ((offset = mouse.h - dumpCWidMax * 6 - kDIST_FROMLEFT) >= 0)
  158.         if ((offset /= dumpCWidMax) % 5 != 4) {
  159.             tempRect.top = (mouse.v /= dumpLineHeight) * dumpLineHeight;
  160.             tempRect.left = ((offset /= 5) * 5) * dumpCWidMax + dumpCWidMax * 6 + kDIST_FROMLEFT;
  161.             tempRect.bottom = tempRect.top + dumpLineHeight;
  162.             tempRect.right = tempRect.left + (dumpCWidMax << 2);
  163.             clickAddr = ((unsigned long)(GetControlValue(dumpVScroll) + mouse.v) << 4)
  164.                         + (offset <<= 1);
  165.             if (popItem = TrackDumpObject(&tempRect, *(unsigned short *)(gMMemory + clickAddr))) {
  166.                 if (popItem == kD_Disasm) {
  167.                     SetControlValue(disasmVScroll, clickAddr >> 2);
  168.                     InvalDisasm();
  169.                     DoMenuWindows(kMItem_Disasm);
  170.                     }
  171.                 else if ((event->modifiers & optionKey)||(popItem == kD_DisasmFrom)) {
  172.                     SetControlValue(disasmVScroll, *(unsigned short *)(gMMemory + clickAddr) >> 1);
  173.                     InvalDisasm();
  174.                     DoMenuWindows(kMItem_Disasm);
  175.                     }
  176.                 else if ((event->modifiers & cmdKey)||(popItem == kD_DumpFrom)) {
  177.                     SetControlValue(dumpVScroll, *(unsigned short *)(gMMemory + clickAddr) >> 3);
  178.                     InvalDump();
  179.                     }
  180.                 else {
  181.                     if (DoEditDump((short *)(gMMemory + clickAddr), clickAddr >> 1)) {
  182.                         InvalDump();
  183.                         InvalDisasm();
  184.                         }
  185.                     UnloadSeg(DoEditDump);
  186.                     }
  187.                 }
  188.             }
  189.     }
  190. else {
  191.     part = FindControl(mouse, w, &control);
  192.     switch ( part ) {
  193.         case 0:        /* do nothing for viewRect case */
  194.             break;
  195.         case kControlIndicatorPart:
  196.             if (gPrefs.NeXTScroll)
  197.                 (void)TrackThumb(control, mouse, RealTimeActionProc);
  198.             else {
  199.                 GetPort(&savePort);
  200.                 curPosW = GetNewWindow(kCurPosWindow, nil, (WindowPtr)-1L);
  201.                 SetPortWindowPort(curPosW);
  202.                 TextMode(srcCopy);
  203.                 TextFont(monaco);
  204.                 TextSize(9);
  205.                 SetPort(savePort);
  206.                 savePort = (GrafPtr)LMGetGhostWindow();
  207.                 LMSetGhostWindow(curPosW);
  208.                 ShowWindow(curPosW);
  209.                 (void) TrackThumb(control, mouse, NoRealTimeActionProc);
  210.                 DisposeWindow(curPosW);
  211.                 LMSetGhostWindow(savePort);
  212.                 InvalDump();
  213.                 }
  214.             break;
  215.         default:    /* clicked in an arrow, so track & scroll */
  216.             {
  217.             ControlActionUPP DumpActionProcUPP = NewControlActionProc(DumpActionProc);
  218.  
  219.             (void) TrackControl(control, mouse, DumpActionProcUPP);
  220.             if (DumpActionProcUPP)
  221.                 DisposeRoutineDescriptor(DumpActionProcUPP);
  222.             }
  223.             break;
  224.         }
  225.     }
  226. }
  227.  
  228. /* NoRealTimeActionProc: only updates the number in the little window up there */
  229.  
  230. static void NoRealTimeActionProc(ControlHandle , short value)
  231. {
  232. Str15    tempS;
  233. GrafPtr    savePort;
  234.  
  235. ShortToHexString(value << 3, tempS);
  236. GetPort(&savePort);
  237. SetPort(curPosW);
  238. MoveTo(kDIST_FROMLEFT,12);
  239. DrawString(tempS);
  240. SetPort(savePort);
  241. }
  242.  
  243. /* RealTimeActionProc: updates all of the window contents while dragging the scroll box */
  244.  
  245. static void RealTimeActionProc(ControlHandle control, short )
  246. {
  247. DrawDump((*control)->contrlOwner);
  248. }
  249.  
  250. static pascal void DumpActionProc(ControlHandle control, short part)
  251. {
  252. Rect    tempRect;
  253. register WindowPtr    w;
  254. register Ptr    addr;
  255. register short    amount, oldAmount, newAmount, vc;
  256. register Boolean    doScrollRect = false;
  257.  
  258. if ( part ) {        /* if it was actually in the control */
  259.     w = (*control)->contrlOwner;
  260.     switch ( part ) {
  261.         case kControlUpButtonPart:
  262.             amount = -1;
  263.             doScrollRect = true;
  264.             break;
  265.         case kControlDownButtonPart:
  266.             amount = 1;
  267.             doScrollRect = true;
  268.             break;
  269.         case kControlPageUpPart:
  270.             amount = (PRCT_T(w) - PRCT_B(w)) / dumpLineHeight + 1;
  271.             break;
  272.         case kControlPageDownPart:
  273.             amount = (PRCT_B(w) - PRCT_T(w)) / dumpLineHeight - 1;
  274.             break;
  275.         }
  276.     SetControlValue(control, (oldAmount = GetControlValue(control)) + amount);
  277.     if (doScrollRect && (newAmount = oldAmount - GetControlValue(control))) {
  278.         tempRect = w->portRect;
  279.         tempRect.right -= kScrollbarAdjust;
  280.         VScrollRect(&tempRect, newAmount * dumpLineHeight);
  281.         addr = &gMMemory[(unsigned long)GetControlValue(control) << 4];
  282.         vc = dumpFromTop;
  283.         if (newAmount < 0) {
  284.             vc += PRCT_B(w) - PRCT_T(w) - dumpLineHeight;
  285.             addr += ((PRCT_B(w) - PRCT_T(w)) / dumpLineHeight - 1) << 4;
  286.             }
  287.         MoveTo(PRCT_L(w) + kDIST_FROMLEFT, vc);
  288.         DrawLine(addr);
  289.         }
  290.     else
  291.         DrawDump(w);
  292.     }
  293. } /* DumpActionProc */
  294.  
  295. void DrawDump(WindowPtr w)
  296. {
  297. Rect    tempRect;
  298. GrafPtr    savePort;
  299. Point    tempPoint;
  300. register Ptr    addr;
  301. register short    j;
  302.  
  303. GetPort(&savePort);
  304. SetPort(w);
  305. tempRect = w->portRect;
  306. tempRect.bottom += dumpLineHeight;
  307. addr = &gMMemory[(unsigned long)GetControlValue(dumpVScroll) << 4];
  308. for(tempPoint.h = kDIST_FROMLEFT, j = dumpFromTop;
  309.     tempPoint.v = j, PtInRect(tempPoint, &tempRect);
  310.     j += dumpLineHeight, addr += 16) {
  311.  
  312.     MoveTo(PRCT_L(w) + kDIST_FROMLEFT, j);
  313.     DrawLine(addr);
  314.     }
  315. SetPort(savePort);
  316. }
  317.  
  318. /* DrawLine: draws a "line" of memory in the Dump window */
  319.  
  320. static void DrawLine(Ptr start)
  321. {
  322. Str63    tempS;
  323. register short *hexPtr, *endPtr;
  324. register Ptr    textPtr;
  325.  
  326. endPtr = (short *)(start + 16);
  327. textPtr = ShortToHexText(PTR2MEMWORD(start), (Ptr)&tempS);
  328. *textPtr++ = ':';
  329. for (hexPtr = (short *)start; hexPtr < endPtr; ) {
  330.     *textPtr++ = ' ';
  331.     textPtr = ShortToHexText(*hexPtr++, textPtr);
  332.     }
  333. DrawText(&tempS, 0, 45);
  334. }
  335.  
  336. /* SetupDumpCtlMax: sets up the CtlMax value of our scroll bar */
  337.  
  338. void SetupDumpCtlMax(ControlHandle theControl)
  339. {
  340. enum {
  341. kAdjustForPleasantGrow = 3
  342. };
  343.  
  344. register WindowPtr    wind;
  345. register short    newmax;
  346.  
  347. wind = (*theControl)->contrlOwner;
  348. newmax = 8191 - (PRCT_B(wind) - PRCT_T(wind) - kAdjustForPleasantGrow) / dumpLineHeight;
  349. if (newmax != GetControlMaximum(theControl)) {
  350.     SetControlMaximum(theControl, newmax);
  351.     InvalDump();
  352.     }
  353. }
  354.  
  355. void InvalDump(void)
  356. {
  357. Rect    tempRect;
  358. GrafPtr    savePort;
  359.  
  360. GetPort(&savePort);
  361. SetPort(gWPtr_Dump);
  362. tempRect = gWPtr_Dump->portRect;
  363. tempRect.right -= kScrollbarWidth;
  364. InvalRect(&tempRect);
  365. SetPort(savePort);
  366. }
  367.  
  368. /* TrackDumpObject: like all the TrackThing in the Mac OS, plus the popUp */
  369.  
  370. short    TrackDumpObject(RectPtr    r, short theLocation)
  371. {
  372. enum {
  373. kSimpleClick = -1,
  374. kDelay = 1 * 60 // one second
  375. };
  376.  
  377. Str32    itemStr;
  378. Str15    numStr;
  379. Point    myPt;
  380. MenuRef    tempMenu;
  381. register long        timeout = TickCount();
  382. register long        chosen = kSimpleClick;
  383. register Boolean    inrect;
  384.  
  385. InvertRect(r);
  386. inrect = true;
  387. do {
  388.     register Boolean tempB;
  389.  
  390.     GetMouse(&myPt);
  391.     if ((tempB = PtInRect(myPt, r)) != inrect) {
  392.         InvertRect(r);
  393.         inrect = tempB;
  394.         }
  395.     if (inrect == false)
  396.         timeout = TickCount();
  397.     if ((TickCount() - timeout) > GetDblTime()) {
  398.         tempMenu = gPopMenu;
  399.         if (noErr == HandToHand((Handle *)&tempMenu)) {
  400.             GetMenuItemText(tempMenu, kD_DisasmFrom, itemStr);
  401.             ShortToHexString(theLocation, numStr);
  402.             PLstrcat(itemStr, numStr);
  403.             SetMenuItemText(tempMenu, kD_DisasmFrom, itemStr);
  404.             GetMenuItemText(tempMenu, kD_DumpFrom, itemStr);
  405.             PLstrcat(itemStr, numStr);
  406.             SetMenuItemText(tempMenu, kD_DumpFrom, itemStr);
  407.             InsertMenu(tempMenu, hierMenu);
  408.             LocalToGlobal(&myPt);
  409.             chosen = PopUpMenuSelect(tempMenu, myPt.v, myPt.h, kD_Edit);
  410.             DeleteMenu(kRes_Menu_PopDump);
  411.             DisposeMenu(tempMenu);
  412.             }
  413.         }
  414.     }
  415. while( StillDown() );
  416. if (inrect)
  417.     InvertRect(r);
  418. if (chosen == kSimpleClick)
  419.     chosen = inrect ? toMenu(kRes_Menu_PopDump, kD_Edit) : 0L;
  420. return HiWrd(chosen) ? LoWrd(chosen) : 0;
  421. }
  422.  
  423. /* procedure called when closing the Dump window */
  424.  
  425. void CloseDump(WindowPtr w)
  426. {
  427. DoCloseWindow(w, kMItem_Dump);
  428. }
  429.  
  430. void RecalcDump(DialogRef w, RgnBalloonCursPtr theObj)
  431. {
  432. Rect    tempRect;
  433.  
  434. tempRect = w->portRect;
  435. tempRect.right -= kScrollbarWidth;
  436. RectRgn(theObj->zoneLocal, &tempRect);
  437. }
  438.  
  439. void getDragRectDump(WindowPtr w, RectPtr r)
  440. {
  441. *r = w->portRect;
  442. r->right -= kScrollbarAdjust;
  443. }
  444.  
  445. #endif
  446.  
  447.